"""
插入种子数据的脚本
"""
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__)))

from database import SessionLocal, engine
from sqlalchemy import text

from models.category import Category

def insert_seed_data():
    """插入种子数据"""
    db = SessionLocal()
    try:
        # 检查是否已经有数据
        category_count = db.query(Category).count()
        
        print(f"当前数据库状态:")
        print(f"- 分类数量: {category_count}")
        
        if category_count == 0:
            print("插入分类数据...")
            # 插入一级分类
            categories = [
                Category(name="数码电子", icon="laptop", description="手机、电脑、相机等数码产品", sort_order=1, is_active=True),
                Category(name="图书教材", icon="book", description="教科书、参考书、小说等", sort_order=2, is_active=True),
                Category(name="服装配饰", icon="shirt", description="衣服、鞋子、包包等", sort_order=3, is_active=True),
                Category(name="生活用品", icon="home", description="日用品、家居用品等", sort_order=4, is_active=True),
                Category(name="体育运动", icon="dribbble", description="运动器材、健身用品等", sort_order=5, is_active=True),
                Category(name="文具办公", icon="edit", description="文具、办公用品等", sort_order=6, is_active=True),
                Category(name="美妆护肤", icon="heart", description="化妆品、护肤品等", sort_order=7, is_active=True),
                Category(name="其他", icon="more", description="其他物品", sort_order=99, is_active=True),
            ]
            db.add_all(categories)
            db.commit()
            
            # 插入二级分类
            categories = [
                # 数码电子子分类
                Category(name="手机通讯", parent_id=1, icon="mobile", description="手机、平板等通讯设备", sort_order=1, is_active=True),
                Category(name="电脑办公", parent_id=1, icon="desktop", description="笔记本、台式机、配件等", sort_order=2, is_active=True),
                Category(name="摄影摄像", parent_id=1, icon="camera", description="相机、摄像头、镜头等", sort_order=3, is_active=True),
                Category(name="数码配件", parent_id=1, icon="usb", description="充电器、数据线、耳机等", sort_order=4, is_active=True),
                
                # 图书教材子分类
                Category(name="专业教材", parent_id=2, icon="book", description="各专业课程教材", sort_order=1, is_active=True),
                Category(name="考试资料", parent_id=2, icon="file-text", description="考研、考证资料", sort_order=2, is_active=True),
                Category(name="课外读物", parent_id=2, icon="book-open", description="小说、散文等课外书籍", sort_order=3, is_active=True),
                
                # 服装配饰子分类
                Category(name="男装", parent_id=3, icon="user", description="男士服装", sort_order=1, is_active=True),
                Category(name="女装", parent_id=3, icon="user", description="女士服装", sort_order=2, is_active=True),
                Category(name="鞋靴", parent_id=3, icon="shoe-prints", description="各类鞋子", sort_order=3, is_active=True),
                Category(name="箱包", parent_id=3, icon="shopping-bag", description="背包、手提包等", sort_order=4, is_active=True),
                
                # 生活用品子分类
                Category(name="宿舍用品", parent_id=4, icon="bed", description="床上用品、收纳等", sort_order=1, is_active=True),
                Category(name="厨房用品", parent_id=4, icon="utensils", description="锅具、餐具等", sort_order=2, is_active=True),
                Category(name="清洁用品", parent_id=4, icon="soap", description="洗衣液、清洁剂等", sort_order=3, is_active=True),
                
                # 体育运动子分类
                Category(name="健身器材", parent_id=5, icon="dumbbell", description="哑铃、瑜伽垫等", sort_order=1, is_active=True),
                Category(name="球类运动", parent_id=5, icon="basketball-ball", description="篮球、足球、羽毛球等", sort_order=2, is_active=True),
                Category(name="户外用品", parent_id=5, icon="mountain", description="帐篷、登山包等", sort_order=3, is_active=True),
            ]
            db.add_all(categories)
            db.commit()
            print("分类数据插入完成")
        
        # 重新检查数据
        category_count = db.query(Category).count()
        
        print(f"插入后数据库状态:")
        print(f"- 分类数量: {category_count}")
        print("种子数据插入完成！")
        
    except Exception as e:
        print(f"插入数据失败: {e}")
        db.rollback()
    finally:
        db.close()

if __name__ == "__main__":
    insert_seed_data()